<?php


namespace app\common\utils;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;


class ExcelHelper
{

    /**
     * @param $exportName 导出文件名
     * @param $cellTitle header
     * @param $cellData 导出数据 二维数组
     * Created by chenyawei on 2024/12/2
     * Description: 浏览器导出excel
     */
     public static function browserExportExcel($exportName ,$cellData){
         if(empty($cellData))return false;
         // 清理输出缓冲区
         ob_clean();
         flush();
         $spreadsheet = new Spreadsheet();
         $sheet =  $spreadsheet->getActiveSheet();
         $sheet->fromArray($cellData);
         foreach ($sheet->getColumnIterator() as $column) {
             $sheet->getColumnDimension($column->getColumnIndex())->setAutoSize(true);
         }
         header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
         header('Content-Disposition: attachment; filename="' . $exportName . '.xlsx"');
         header('Cache-Control: max-age=0');
         $xlsx = new Xlsx($spreadsheet);
         $xlsx->save('php://output');
         flush();
     }

 

    /**获取列字母
     * @param $num
     * @return string
     */
    public static function getChar($num)
    {
        $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $length = strlen($str);
        $rightpos = $num % $length; //个位数
        $leftpos = floor($num / $length);  //十位数
        if ($leftpos > 0) {
            return $str[$leftpos - 1] . $str[$rightpos];
        }
        else {
            return $str[$rightpos];
        }
    }
     
}